#include "cknxcontrol.h"
#include "knx/cmei.h"

CExecStruct::CExecStruct(QObject *parent) :
    QObject(parent)
{
    type = ONE_BIT_VALUE ;
}

CKnxControl::CKnxControl(QObject *parent) :
    QObject(parent)
{
}

int CKnxControl::parseNumberFromString(QString num)
{
    bool status = false;
    float v = num.toFloat(&status);
    if(status)
    {
        return (int)v;
    }

    return 0;
}

QString CKnxControl::foundKeyFromExp(QString exp,QString key)
{
    QStringList l = exp.split(";");
    for(int i =0;i<l.size();i++)
    {
        QString s = l.at(i);
        if(s.startsWith(key))
        {
            QStringList sub_sl = s.split("=");
            if(sub_sl.size() == 2)
                return sub_sl.at(1);
        }
    }

    return "";
}

void CKnxControl::parseSelection(QString selection)
{

}

void CKnxControl::parseExp(QString exp)
{
//    QString location_msg = foundKeyFromExp(exp,"location");
//    if(!location_msg.isEmpty())
//    {
//        QStringList list = location_msg.split(",");
//        if(list.size() == 2)
//        {
//            _floor = list.at(0);
//            _location = list.at(1);
//        }
//    }
    QStringList exp_msg = exp.split(";");
    if(exp_msg.size() >= 2)
    {
        _status_true = exp_msg.at(0);
        _status_false = exp_msg.at(1);
    }

    if(exp_msg.size() >= 4)
    {
        _min_value = exp_msg.at(2);
        _max_value = exp_msg.at(3);
    }
}

void CKnxControl::parseZone(QString zone)
{
    QStringList location_msg = zone.split(";");
    if(location_msg.size()>0)
    {
        _area = location_msg.at(0);
    }
}

QString CKnxControl::valueCheck(QString action, QString value)
{
    return value;
}

CKnxExecThread::CKnxExecThread(QThread *parent):
    QThread(parent)
{
    ttgConnector = NULL;
}

void CKnxExecThread::run()
{
    if(!ttgConnector)
    {
        qDebug() << "KNX Connector not open!";
        return;
    }

    if(execList.size() == 0)
        qDebug() << "CKnxExecThread: exec when size is zero !";

    connect(this,SIGNAL(s_knxRead(QByteArray)),ttgConnector,SLOT(doReadDevice(QByteArray)));
    connect(this,SIGNAL(s_knxWrite(QByteArray,int,QByteArray)),
            ttgConnector,SLOT(doWriteDevice(QByteArray,int,QByteArray)));

    int size = execList.size();
    unsigned char _addr_buf[2];

    for(int i=0;i<size;i++)
    {
        const CExecStruct *es = execList.at(i);
        if(es->address.startsWith("msleep"))
        {
            QStringList list = es->address.split(":");
            if(list.size()>=4)
            {
                unsigned int time = list.at(3).toUInt();
                if(time > 1000*60)
                    time = 1000*60;

                usleep(1000*time);
            }
        }
        else
        {
            if(es->data.size()>0)
            {
                qDebug() << "KNX: write address " << es->address;
                cmei_set_group_address(_addr_buf,es->address.toLocal8Bit());

                QByteArray addr2Bytes((const char *)_addr_buf,2);
                emit s_knxWrite(addr2Bytes,es->type,es->data);

                //     ttgConnector->ttg_write_device(_addr_buf,es->type,
                //            (unsigned char *)es->data.data(),es->data.size());
            }
            else
            {
                qDebug() << "KNX: read address " << es->address;

                cmei_set_group_address(_addr_buf,es->address.toLocal8Bit());

                QByteArray addr2Bytes((const char *)_addr_buf,2);
                emit s_knxRead(addr2Bytes);
            }
        }

        //delete es;
    }

    sleep(1);
    execList.clear();
}
